#-*- encoding:utf-8 -*-
import numpy as np
import sys;
import matplotlib;
import matplotlib.pyplot as plt
import struct
reload(sys)
sys.setdefaultencoding('utf8')
matplotlib.rcParams['axes.unicode_minus'] = False
slice_size=31
trace_num=200
num_point=200
add_point=31
side_add_point=(add_point-1)/2
true_set_num=150
false_set_num=200


def get_true_data():
    x_number=400;
    y_number=31;
    data_true=np.zeros((x_number,y_number))
    f=open('data/train_data_1.bin','rb')
    for i in range(400):
        for j in range(31):
            data = f.read(4)
            data_float=struct.unpack('f',data)[0]
            data_true[i][j]=data_float
    return data_true

def get_false_data():
    x_number=600;
    y_number=31;
    data_false=np.zeros((x_number,y_number))
    f=open('data/train_data_0.bin','rb')
    for i in range(x_number):
        for j in range(y_number):
            data = f.read(4)
            data_float=struct.unpack('f',data)[0]
            data_false[i][j]=data_float
    return data_false

def get_ture_label():
    x_number = 400;
    true_label=np.zeros((x_number,1))
    true_label[:,0]=1
    return true_label

def get_false_label():
    x_number = 600;
    false_label = np.zeros((x_number, 1))
    false_label[:, 0] = 0
    return false_label

def get_set_and_label():
    set1=get_true_data()
    label1=get_ture_label()
    set2=get_false_data()
    label2=get_false_label()
    train_x=np.concatenate([set1,set2],axis=0)
    label_y=np.concatenate([label1,label2],axis=0)
    return train_x,label_y
def get_test_trace(filename):
    x_number=400;
    y_number=200;
    data=np.zeros((x_number,y_number))
    f=open(filename,'rb')
    for i in range(x_number):
        for j in range(y_number):
            temp = f.read(4)
            data_float=struct.unpack('f',temp)[0]
            data[i][j]=data_float
    return data

def get_single_noise10__data():
    f = open('data/noise10_single_data.txt', 'r')
    text = f.read()
    f.close()
    test_list = text.split('\r\n')
    data_set = []
    for i in range(trace_num):
        data_set.append(float(test_list[i]))
    data_single = np.array(data_set)
    arr_request = np.zeros((num_point, slice_size), dtype=float)
    add_data1 = np.zeros(side_add_point, dtype=float)
    add_data2 = np.zeros(side_add_point, dtype=float)
    add_data1[:] = data_single[0:side_add_point]
    add_data2[:] = data_single[-side_add_point:]
    data_new = np.append(add_data1, data_single)
    data_new = np.append(data_new, add_data2)
    data_new = np.abs(data_new)
    max = np.max(data_new)
    min = np.min(data_new)
    data_new = (data_new - min) / (max - min)
    for i in range(num_point):
        arr_request[i, :] = data_new[i:i + add_point]
    return arr_request, data_single
def get_fb_ffid1_in_litter_windows():
    y_number = 400;
    data_fb= np.zeros(y_number)
    f = open('data/fb_ffid1_in_little_window.bin', 'rb')
    for j in range(y_number):
        data = f.read(4)
        data_float = struct.unpack('f', data)[0]
        data_fb[j] = data_float
    return data_fb

if __name__=='__main__':
    x=get_test_trace(1)
    print x.shape
